﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using CRM.Entity;
using CRM.BLL;
using DP.Web.UI;
using DP.Common;
using System.Text;

namespace CRM.Web.SurveyManage
{
    public partial class ItemsOptionsModify : Common.PageBase
    {
        T_SURVEY_ITEMS_BLL itemsBll = new T_SURVEY_ITEMS_BLL();
        T_SURVEY_OPTIONS_BLL optionsBll = new T_SURVEY_OPTIONS_BLL();
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            PageHelper.SetPrePageUrl(this.Page);
            PageHelper.SetGoBackUrl(this.Page, btnCancel, StaticConstant.NotBackUrl);
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                pageDataInit();
            }
        }
        #region 页面初始化数据绑定
        #endregion

        private void pageDataInit()
        {
            string _autoid = String.IsNullOrEmpty(Request.QueryString["AUTOID"]) ? "" : Request.QueryString["AUTOID"].Trim();
            string _itemid = String.IsNullOrEmpty(Request.QueryString["ITEM_ID"]) ? "" : Request.QueryString["ITEM_ID"].Trim();
            if (!String.IsNullOrEmpty(_autoid))
            {
                T_SURVEY_OPTIONS_Entity entity = optionsBll.GetEntity("AUTOID", _autoid);
                PageHelper.SetControlValue<T_SURVEY_OPTIONS_Entity>(this.Page, entity);
                lblTitle.Text = "选项信息修改";
            }
            else
            {
                T_SURVEY_ITEMS_Entity entity = itemsBll.GetEntity("AUTOID", _itemid);
                if (entity != null)
                {
                    txtITEM_ID.Text = entity.AUTOID.ToString();
                    txtITEM_NAME.Text = entity.ITEM_NAME;
                }
                string optionid = optionsBll.GetMaxValue("OPTION_ID", " AND ITEM_ID='" + _itemid + "'");
                int i = 1;
                if (!String.IsNullOrEmpty(optionid))
                {
                    i = StringHelper.ToInt(optionid) + 1;
                }
                txtOPTION_ID.Text = i.ToString();
                txtOPTION_NAME.Text = "";
                lblTitle.Text = "选项信息添加";
            }
        }
        protected void btnOK_Click(object sender, EventArgs e)
        {
            T_SURVEY_OPTIONS_Entity entity = new T_SURVEY_OPTIONS_Entity();
            if (txtOPTION_NAME.Text == "")
            {
                PageHelper.ShowMessage(this.Page, "选项名称不能为空！");
                return;
            }
            entity = optionsBll.GetEntity("AUTOID", txtAUTOID.Text);
            if (entity == null)
            {
                entity = optionsBll.GetEntity("OPTION_NAME", txtOPTION_NAME.Text);
                if (entity == null)
                {
                    entity = new T_SURVEY_OPTIONS_Entity();
                    PageHelper.GetControlValue<T_SURVEY_OPTIONS_Entity>(this.Page, ref entity);
                    entity.BUSINESSDIVISIONCODE = _userLoginInfo.UserEntity.BUSINESSDIVISIONCODE;
                    entity.BUSINESSDIVISIONID = _userLoginInfo.UserEntity.BUSINESSDIVISIONID;
                    entity.BUSINESSDIVISIONNAME = _userLoginInfo.UserEntity.BUSINESSDIVISIONNAME;
                    optionsBll.Insert(entity);
                }
                else
                {
                    PageHelper.ShowMessage(this.Page, "选项名称已经存在，请核实后再进行增加");
                    return;
                }
            }
            else
            {
                PageHelper.GetControlValue<T_SURVEY_OPTIONS_Entity>(this.Page, ref entity);
                optionsBll.Update(entity);
            }
            PageHelper.ShowMessage(this.Page, "保存成功！");
            Response.Redirect(String.Format("ItemsOptionsList.aspx?ITEM_ID={0}", txtITEM_ID.Text));

        }
    }
}
